/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.mp.comment.platform.controller;

import com.yami.shop.mp.comment.model.WeixinSubscribe;
import com.yami.shop.mp.comment.service.WeixinSubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * 微信关注时的回复
 * @author lgh on 2019/01/16.
 */
@RestController
@RequestMapping("/weixin/subscribe")
public class WeixinSubscribeController {

    @Autowired
    private WeixinSubscribeService weixinSubscribeService;


	/**
	 * 分页获取
	 */
    @GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('weixin:subscribe:page')")
	public ResponseEntity<List<WeixinSubscribe>> page(){
    	List<WeixinSubscribe> list =  weixinSubscribeService.listWeixinSubscribeAndMaterial();
    	for (WeixinSubscribe weixinSubscribe : list) {
    		weixinSubscribe.setContent(weixinSubscribe.getContent());
		}
		return ResponseEntity.ok(list);
	}

	/**
	 * 保存
	 */
	@PostMapping
	@PreAuthorize("@pms.hasPermission('weixin:subscribe:save')")
	public ResponseEntity<Void> save(@RequestBody @Valid WeixinSubscribe weixinSubscribe){
		weixinSubscribe.setContent(weixinSubscribe.getContent());
		weixinSubscribe.setCreateTime(new Date());
		weixinSubscribeService.save(weixinSubscribe);
		weixinSubscribeService.removeWeixinSubscribeAndMaterial();
		return ResponseEntity.ok().build();
	}

	/**
	 * 修改
	 */
	@PutMapping
	@PreAuthorize("@pms.hasPermission('weixin:subscribe:update')")
	public ResponseEntity<Void> update(@RequestBody @Valid WeixinSubscribe weixinSubscribe){
		weixinSubscribe.setContent(weixinSubscribe.getContent());
		weixinSubscribeService.updateById(weixinSubscribe);
		weixinSubscribeService.removeWeixinSubscribeAndMaterial();
		return ResponseEntity.ok().build();
	}

	/**
	 * 删除
	 */
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('weixin:subscribe:delete')")
	public ResponseEntity<Void> delete(@PathVariable Long id){
		weixinSubscribeService.removeById(id);
		weixinSubscribeService.removeWeixinSubscribeAndMaterial();
		return ResponseEntity.ok().build();
	}
}
